package com.extract;

/**
 * @description: 以函数取代参数
 * @author: cong zhi
 * @createDate: 2022/5/25 8:51
 * @updateUser: cong zhi
 * @updateDate: 2022/5/25 8:51
 * @updateRemark: 修改内容
 * @version: v1.0
 */
public class Goods {

    private int quantiy;

    private int itemPrice;




  /*  public double getPrice() {
        int basePrice = getDiscountLevel(quantiy) * itemPrice;
        int discountLevel;
        if (quantiy > 100) {
            discountLevel = 2;
        } else {
            discountLevel = 1;
        }
        double finalPrice = discountedPrice(basePrice, discountLevel);
        return finalPrice;
    }*/

    private double discountedPrice(int basePrice, int discountLevel) {
        if (discountLevel == 2) {
            return basePrice * 0.1;
        } else {
            return basePrice * 0.05;
        }
    }

    /**
     * 将discountedPrice()函数中对discountLevel参数的所有引用点，替换为对getDiscountLevel()函数的调用
     *
     * @param basePrice
     * @return
     */
    private double discountedPrice(int basePrice) {
        if (getDiscountLevel() == 2) {
            return basePrice * 0.1;
        } else {
            return basePrice * 0.05;
        }
    }

    /**
     * 去掉非必要的参数和想相应的临时变量
     *
     * @param basePrice
     * @return
     */
    private double discountedPrice() {
        if (getDiscountLevel() == 2) {
            return getBasePrice() * 0.1;
        } else {
            return getBasePrice() * 0.05;
        }
    }
    /**
     * 重构提炼计算折扣规则
     *
     * @return
     */
    public double calcPrice() {

        int basePrice = quantiy * itemPrice;
        int discountLevel = getDiscountLevel();
        double finalPrice = discountedPrice(basePrice, discountLevel);
        return finalPrice;
    }

    private int getBasePrice() {
        return quantiy * itemPrice;
    }


    /**
     * 重构:将discountLevel变量去除
     *
     * @return
     */
    public double getPrice() {
        return discountedPrice();
    }

    /**
     * 重构:将discountLevel变量去除
     *
     * @return
     */
    public double calPrice() {
        if (getDiscountLevel() == 2) {
            return getBasePrice() * 0.1;
        } else {
            return getBasePrice() * 0.05;
        }
    }
    /**
     * 将提炼计算折扣等级提炼成一个独立的 getDiscountLevel（）函数
     *
     * @param basePrice
     * @return
     */
    private int getDiscountLevel() {
        if (quantiy > 100) {
            return 2;
        } else {
            return 1;
        }
    }
}
